package com.chaoyi.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.Dictionary;
import com.chaoyi.service.DictionaryService;

/**
 * 数据字典管理
 * 
 * @author wbf
 */
@Controller
@RequestMapping("/dictionary")
public class DictionaryController {

	@Resource
	private DictionaryService dictionaryService;

	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ModelAndView mod = new ModelAndView("/dictionary/dictionaryList");
		return mod;
	}

	/**
	 * 添加父字典数据页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/add_plus")
	public ModelAndView add_plus(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("/dictionary/dictionary_add");
		return mv;
	}

	/**
	 * 跳转子页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/addChild_plus")
	public ModelAndView addChild_plus(HttpServletRequest request, HttpServletResponse response, int parentId) {
		ModelAndView mv = new ModelAndView("/dictionary/dictionaryChild_add");
		mv.addObject("obj", parentId);
		return mv;
	}

	/**
	 * 父列表展示
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index_json")
	@ResponseBody
	public JSONObject index_json(HttpServletRequest request, HttpServletResponse response) {
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		List<Dictionary> dictionaryList = dictionaryService.findList(offset, limit);
		JSONObject json = new JSONObject();
		json.put("rows", dictionaryList);
		json.put("total", dictionaryList.size());
		return json;
	}

	/**
	 * 子列表展示
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/children_json")
	@ResponseBody
	public JSONObject children_json(HttpServletRequest request, HttpServletResponse response, int parentId) {
		List<Dictionary> dictionaryList = dictionaryService.findChildrenList(parentId);
		JSONObject json = new JSONObject();
		json.put("rows", dictionaryList);
		json.put("total", dictionaryList.size());
		return json;
	}

	/**
	 * 保存新增父节点
	 * 
	 * @param dictionaryName
	 * @param dicId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public JSONObject add(HttpServletRequest request, HttpServletResponse response, Dictionary dictionary) {
		JSONObject json = new JSONObject();
		int number = dictionaryService.saveDictionaryParent(dictionary);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}

	/**
	 * 保存新增子节点
	 * 
	 * @param dictionaryName
	 * @param dicId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("chile_add")
	public JSONObject chile_add(HttpServletRequest request, HttpServletResponse response, Dictionary dictionary) {
		JSONObject json = new JSONObject();
		int number = dictionaryService.saveDictionaryChile(dictionary);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}

	/**
	 * 验证字典名称是否重复
	 * 
	 * @param dictionaryName
	 * @param id
	 * @return JSONObject
	 */
	@ResponseBody
	@RequestMapping("checkDictionaryName")
	public JSONObject checkDictionaryName(HttpServletRequest request, HttpServletResponse response,
			String dictionaryName, int dicId) {
		JSONObject json = new JSONObject();
		int number = dictionaryService.checkDictionaryName(dictionaryName, dicId);
		if (number != 0) {
			json.put("valid", false);
			return json;
		}
		json.put("valid", true);
		return json;
	}

}
